// pages/car/startcar/index.ts
import {saveCar,Car} from '../../../api/car'
import { LENGYU_SUCCESS_CODE } from '../../../utils/request';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    needItems:[ 
      {text: '带小号', value:  '带小号'},
      {text: '同水平速刷', value: '同水平速刷'},
      {text: '凑人数', value: '凑人数'},
      {text: '求大号带', value: '求大号带'},
      {text: '纯躺尸', value: '纯躺尸'},
    ],
    showActionsheet: false,
    error:"",
    formData: {
    },
    rules: [
      {
        name: 'need',
        rules: {required: true, message: '请选择目标'},
      },
      {
          name: 'carDate',
          rules: {required: true, message: '请选择日期'},
      },
      {
          name: 'carTime',
          rules: {required: true, message: '请选择时间'},
      },
      {
          name: 'playHours',
          rules: {required: false, message: '请填写小时数'},
      },
      {
        name: 'gs',
        rules: {required: true, message: '你还没有填装等'},
      },
      {
        name: 'career',
        rules: {required: true, message: '你还没选职业'},
      },
      {
        name: 'position',
        rules: {required: true, message: '你还没选天赋'},
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  showActionSheet(showType:number){
    if(showType==1){
      //对应需求的选择
    }
    this.setData({
      showActionsheet: true
  })
  },
  actionSheetClick(e:any){
    console.log(e);
    this.setData({
      'formData.need': e.detail.value
    })
    console.log(this.data.formData)
    this.closeActionSheet();
  },
  closeActionSheet() {
    this.setData({
        showActionsheet: false
    })
  },
  bindDateChange(e:any){
    this.setData({
      'formData.carDate':e.detail.value
    })
  },
  bindTimeChange(e:any){
    this.setData({
      'formData.carTime':e.detail.value
    })
  },
  bindCareerChange(e:any){
    console.log("自定义组件事件",e)
    this.setData({
      'formData.career':e.detail.value[0],
      'formData.position':e.detail.value[1],
    })
  },
  bingPlayHoursChange(e:any){
    this.setData({
      'formData.playHours':e.detail.value
    })
  },
  bingGsChange(e:any){
    this.setData({
      'formData.gs':e.detail.value
    })
  },
  bingTargetLevelChange(e:any){
    this.setData({
      'formData.targetLevel':e.detail.value
    })
  },
  bingProvideChange(e:any){
    this.setData({
      'formData.provide':e.detail.value
    })
  },
 async submitForm(){
    this.selectComponent('#form').validate( async (valid:any, errors:any) =>{
      console.log('valid', valid, errors)
      if (!valid) {
          const firstError = Object.keys(errors)
          if (firstError.length) {
              this.setData({
                  error: errors[firstError[0]].message
              })
              wx.showToast({
                title: this.data.error,
                icon: 'error',
                duration: 2000
              })
          }
      } else {
         let result = await saveCar(this.data.formData as Car);
         if(result.code==LENGYU_SUCCESS_CODE){
          wx.navigateBack()
         }
      }
  })
  }
})